<?php

/**
 * Error Handling Controller
 *
 * Controller used by ErrorHandler to render error views.
 *
 * PHP versions 5
 * CAKEPHP versions 2.x
 * 
 * Green CMS - Content Management System and Framework Powerfull by Cakephp
 * Copyright 2012, GREEN GLOBAL CO., LTD (toancauxanh.vn)
 * 
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @author        Technology Lab No.I <tech1@toancauxanh.vn>
 * @link          
 * @package       Green.Controller
 * @since         Green v 1.0
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
class CakeErrorController extends Controller {

    /**
     * Controller name
     *
     * @var string
     */
    public $name = 'CakeError';

    /**
     * cacheAction Property
     *
     * @var mixel array
     */
    public $cacheAction = false;

    /**
     * viewPath Property
     *
     * @var string
     */
    public $viewPath = 'Errors';

    /**
     * Uses Property
     *
     * @var array
     */
    public $uses = array();

    /**
     * __construct
     *
     * @param CakeRequest $request
     * @param CakeResponse $response
     */
    public function __construct($request = null, $response = null) {
        parent::__construct($request, $response);
        $this->constructClasses();
        $this->Components->trigger('initialize', array(&$this));
        $this->_set(array('cacheAction' => false, 'viewPath' => 'Errors'));
    }

    /**
     * Customesize errors.
     *
     * @param Exception $exception Exception
     * @return void
     */
    function appError(Exception $exception) {
        $template = Inflector::variable(str_replace('Exception', '', get_class($exception)));
        $message = $exception->getMessage();
        $url = $this->request->here();

        if (Configure::read('debug') == 0 && preg_match('/^(missing|scaffold|private|pdo)/i', $template)) {
            $template = 'error400';
            $message = __d('green', 'Not Found');
        }

        $code = $exception->getCode();
        if ($exception instanceof PDOException) {
            $code = 500;
            if ($template !== 'error400') {
                $template = 'pdoError';
            }
        }

        $this->response->statusCode($code);

        if (method_exists($exception, 'getAttributes')) {
            $this->set($exception->getAttributes());
        }

        $this->set(array(
            'name' => $message,
            'url' => h($url),
            'error' => $exception
        ));

        try {
            $this->_outputMessage($template);
        } catch (Exception $ex) {
            if (Configure::read('debug') == 0) {
                $this->set('name', __d('green', 'An Internal Error Has Occurred'));
            }
            $this->_outputMessage('error500');
        }
    }

    /**
     * Send errors message.
     *
     * @param string $template The template to render.
     * @return void
     */
    function _outputMessage($template) {
        $this->render($template);
        $this->shutdownProcess();
        $this->response->send();
    }

}
